package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.ReportauthorMapper;
import com.hbnu.system.model.entity.Report;
import com.hbnu.system.model.entity.Reportauthor;
import com.hbnu.system.model.entity.User;
import com.hbnu.system.service.IReportService;
import com.hbnu.system.service.IReportauthorService;
import com.hbnu.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@Service
public class ReportauthorServiceImpl extends ServiceImpl<ReportauthorMapper, Reportauthor> implements IReportauthorService {

    @Autowired
    IReportauthorService iReportauthorService;
    @Autowired
    IReportService iReportService;
    @Autowired
    IUserService iUserService;
    @Override
    public boolean addReportAuthor(Reportauthor reportAuthor) {
        if (reportAuthor.getUserAccount().isEmpty()){
            throw new BaseException("请输入工号");
        }

        User userByAccount = iUserService.getUserByAccount(reportAuthor.getUserAccount());
        if (!(userByAccount.getName().equals(reportAuthor.getName()))){
            throw new BaseException("输入工号信息与姓名不匹配!");
        }
        Reportauthor pa1 = iReportauthorService.getOne(Wrappers.lambdaQuery(Reportauthor.class)
                .eq(Reportauthor::getReportID,reportAuthor.getReportID())
                .eq(Reportauthor::getUserAccount,reportAuthor.getUserAccount()));
        if(Objects.nonNull(pa1)){
            throw new BaseException("不可重复添加");
        }

        Report report = iReportService.getOne(Wrappers.lambdaQuery(Report.class)
                .eq(Report::getReportID, reportAuthor.getReportID()));
        if(Objects.isNull(report)){
            throw new BaseException("报告不存在!");
        }
        String allauthor = report.getAllAuthor();
        if(StrUtil.isBlank(allauthor)||StrUtil.isEmpty(allauthor)){
            report.setAllAuthor(reportAuthor.getName());
        }else if(report.getAllAuthor().contains(reportAuthor.getName())){
            throw new BaseException("不可重复添加!");
        }else {report.setAllAuthor(allauthor + "、"+reportAuthor.getName());}
//        if(allauthor.isEmpty()||allauthor.equals(" ")){
//            paper.setAllAuthor(paperAuthor.getName());
//        }else {
//            paper.setAllAuthor(allauthor + "、"+paperAuthor.getName());
//        }
        //把相应论文paper进行修改使用update接口 更新它的相关项目这一字段
        UpdateWrapper<Report> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ReportID",report.getReportID());
        iReportService.update(report,updateWrapper);
        return baseMapper.insert(reportAuthor)==1;
    }

    @Override
    public boolean deleteReportAuthor(String id) {
        Reportauthor reportauthor = super.getOne(Wrappers.lambdaQuery(Reportauthor.class)
                .eq(Reportauthor::getReportAuthorID, id));
        if(Objects.isNull(reportauthor)){
            throw new BaseException("不存在关联作者");
        }

        Report report = iReportService.getOne(Wrappers.lambdaQuery(Report.class)
                .eq(Report::getReportID, reportauthor.getReportID()));
        //相关联的Paper里面的字段也要进行修改 此处先把要删除的项目名称提取出来
        String paperauthor_name = reportauthor.getName();
        //此处把Paper里的相关项目的字段值提取出来
        String allAuthor = report.getAllAuthor();
        //把项目名替换成空白即可 分三种情况 分别是顿号在前在后和不存在顿号
        String front = "、" + paperauthor_name;
        String behind = paperauthor_name + "、";
        String both = "、" + paperauthor_name + "、";
        if(allAuthor.contains(both)){
            String replace = allAuthor.replace(both, "、");
            report.setAllAuthor(replace);
        }
        else if(allAuthor.contains(front) && !allAuthor.contains(behind)){
            String replace = allAuthor.replace(front, "");
            report.setAllAuthor(replace);
        }
        else if (allAuthor.contains(behind) && !allAuthor.contains(front)){
            String replace = allAuthor.replace(behind, "");
            report.setAllAuthor(replace);
        }
        else if(allAuthor.contains(paperauthor_name)){
            String replace = allAuthor.replace(paperauthor_name,"");
            report.setAllAuthor(replace);
        }

//        if(associated_projects.contains(front)){
//            String replace = associated_projects.replace(front, "");
//            paper.setAssociatedProjects(replace);
//        }
//        if(associated_projects.contains(behind)){
//            String replace = associated_projects.replace(behind, "");
//            paper.setAssociatedProjects(replace);
//        }
//        if (associated_projects.contains(project_name)){
//            String replace = associated_projects.replace(project_name, "");
//            paper.setAssociatedProjects(replace);
//        }

        //对paper进行更新
        UpdateWrapper<Report> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ReportID",report.getReportID());
        iReportService.update(report,updateWrapper);

        return baseMapper.deleteById(reportauthor) == 1;
    }

    @Override
    public boolean updateReportAuthor(Reportauthor reportauthor) {
        return baseMapper.updateById(reportauthor)==1;
    }

    @Override
    public List<Reportauthor> pagereportauthor(String id) {
        List<Reportauthor> pa = baseMapper.selectList(Wrappers.lambdaQuery(Reportauthor.class)
                .eq(Reportauthor::getReportID,id));
        return pa;
    }
}
